#include <iostream>  
#include <vector>  
#include <string>  
  
using namespace std;  
  
const int SIZE = 15;  
vector<vector<char>> board(SIZE, vector<char>(SIZE, '.')); // 初始化棋盘  
  
// 打印棋盘  
void printBoard() {  
    for (int i = 0; i < SIZE; ++i) {  
        for (int j = 0; j < SIZE; ++j) {  
            cout << board[i][j] << " ";  
        }  
        cout << endl;  
    }  
}  
  
// 检查位置是否有效  
bool isValidPosition(int row, int col) {  
    return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == '.';  
}  
  
// 玩家下棋  
void playMove(int row, int col, char player) {  
    if (isValidPosition(row, col)) {  
        board[row][col] = player;  
    } else {  
        cout << "Invalid move. Try again." << endl;  
    }  
}  
  
// 检查是否获胜（仅检查水平、垂直和对角线）  
bool checkWin(int row, int col, char player) {  
    // 水平检查  
    for (int i = 0; i < 4; ++i) {  
        if (col + i + 1 < SIZE && board[row][col + i] == board[row][col + i + 1] && board[row][col + i] == player) {  
            return true;  
        }  
    }  
  
    // 垂直检查  
    for (int i = 0; i < 4; ++i) {  
        if (row + i + 1 < SIZE && board[row + i][col] == board[row + i + 1][col] && board[row + i][col] == player) {  
            return true;  
        }  
    }  
  
    // 主对角线检查  
    if (row - 4 >= 0 && col - 4 >= 0) {  
        for (int i = 0; i < 4; ++i) {  
            if (board[row - i - 1][col - i - 1] == board[row - i][col - i] && board[row - i][col - i] == player) {  
                return true;  
            }  
        }  
    }  
  
    // 副对角线检查  
    if (row - 4 >= 0 && col + 4 < SIZE) {  
        for (int i = 0; i < 4; ++i) {  
            if (board[row - i - 1][col + i] == board[row - i][col + i + 1] && board[row - i][col + i + 1] == player) {  
                return true;  
            }  
        }  
    }  
  
    return false;  
}  
  
// 游戏主逻辑  
int main() {  
    char currentPlayer = 'X';  
    bool gameOver = false;  
  
    cout << "Gomoku Game (五子棋) Start!" << endl;  
    printBoard();  
  
    while (!gameOver) {  
        int row, col;  
        cout << "Player " << currentPlayer << "'s turn. Enter row and column (e.g., 5 5): ";  
        cin >> row >> col;  
  
        playMove(row - 1, col - 1, currentPlayer); // 转换为0-based index  
        printBoard();  
  
        if (checkWin(row - 1, col - 1, currentPlayer)) {  
            cout << "Player " << currentPlayer << " wins!" << endl;  
            gameOver = true;  
        }  
  
        currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; // 切换玩家  
    }  
  
    return 0;  
}